package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import controller.ByGroupController;
import controller.ByNameController;
import controller.DeleteController;
import controller.LastAddedController;
import controller.ModifyController;
import controller.SendEmailController;
import controller.WebsiteController;
import model.Contact;

/**
 * This class displays the profile of a contact at the right of the window
 * @author Marie Morin
 *
 */

public class ContactProfile extends JPanel{
	
	private final JButton b_modify = new JButton("Modify");
	private final JButton b_delete = new JButton("Delete");
	private MainWindow w;

	
	public ContactProfile(Contact c, MainWindow w) throws URISyntaxException{

		this.w = w;
		
		
		
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		
		JPanel p = new JPanel();
		
		JLabel aname = new JLabel("<html><b><font size='4'>"+c.getAname()+"</font></b></html>");
		JLabel familyName = new JLabel("<html><b>Family name : </b> "+c.getName()+"</html>");	
		JLabel firstName = new JLabel("<html><b>First name : </b> "+c.getFname()+"</html>");
		JLabel phone = new JLabel("<html><b>Phone number : </b> "+c.getPhone()+"</html>");
		JLabel professionalPhone = new JLabel("<html><b>Company phone : </b>"+c.getProfessionalPhone()+"</html>");	
		JLabel homeAddress = new JLabel("<html><b>Home address: </b></br> "+c.getHomeAddress()+"</html>");
		JLabel professionalAddress = new JLabel("<html><b>Company address : </b> "+c.getProfessionalAddress()+"</html>");
		JLabel email = new JLabel("<html><b>Email : </b> <u>"+c.getEmail()+"</u></html>");
		JLabel group = new JLabel("<html><b>Group : </b> "+c.getGroup()+"</html>");
		JLabel date = new JLabel("<html><b>Added : </b> "+c.getDate()+"</html>");
		
		JLabel website = new JLabel("<html><b>Website : </b> <u>"+c.getWebsite()+"</u></html>");
		
		
		if (!c.getWebsite().equals("-")){
			final URI uri = new URI("http://"+c.getWebsite());
			website.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			website.addMouseListener(new WebsiteController(uri));
		}
		if (!c.getEmail().equals("-")){

			final URI uri1 = new URI("mailto:"+c.getEmail());

			email.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			email.addMouseListener(new SendEmailController(uri1));
		}

		
		

		p.setLayout(new GridLayout(20, 1,5,5));
		
		p.add(new JLabel(""));
		p.add(aname);	

		p.add(new JLabel(""));
		
		p.add(phone);
		p.add(homeAddress);
		
		p.add(new JLabel(""));
		p.add(email);
		p.add(website);
	
		p.add(new JLabel(""));
		p.add(group);
		p.add(date);
		
		p.add(new JLabel(""));
		p.add(professionalPhone);
		p.add(professionalAddress);	

		
		JPanel pan = new JPanel();

		pan.add(b_delete);
		pan.add(b_modify);
		
		JLabel label = new JLabel("  ");
		panel.add(label, BorderLayout.WEST);
		
		
		panel.add(p, BorderLayout.CENTER);
			
		this.setLayout(new BorderLayout());
		this.add(panel, BorderLayout.NORTH);
		this.add(pan, BorderLayout.SOUTH);
		
		
		/* In the following code, the purpose is to change what the window displays :
		 * - by refreshing the contact list on the left
		 * - by displaying the profile of the contact select (that is what this class is meant to do)
		*/	
		
		// Refresh the window of the contact list

		if (w.getStaticSearch()=="name")
		{
			new ByNameController(w, w.getStaticWord());
		}
		else if (w.getStaticSearch()=="lastAdded")
		{
			new LastAddedController(w);
		}
		else if (w.getStaticSearch()=="group"){
			
			new ByGroupController(w, w.getStaticWord());
		}
		else if (w.getStaticSearch()=="clear"){
			
			this.w.setLayoutNull();
		}
		
		// Display the contact profile at the right of the window
		w.setWindowProfile(this);
		
		
		b_delete.addActionListener(new DeleteController(c,w));
		b_modify.addActionListener(new ModifyController(c,w));
		
			
	
	}
	


}
